//
//  XMMenuController.m
//  XiaoMiao
//
//  Created by 23 on 2017/11/13.
//  Copyright © 2017年 smallyou. All rights reserved.
//

#import "XMMenuController.h"

@interface XMMenuController () <XMMenuViewDelegate, XMMenuViewProtocol, UIScrollViewDelegate>

/**菜单栏*/
@property(nonatomic,weak) XMMenuView *menuView;
/**scorllView*/
@property(nonatomic,weak) UIScrollView *scrollView;
/**菜单栏指示器*/
@property(nonatomic,weak) UIView *indicatorView;
/**separtor*/
@property(nonatomic,weak) UIView *separtorView;

#pragma mark - 属性
/**当前选中的子控制器索引*/
@property(nonatomic,assign) NSUInteger selectedIndex;

/**是否是第一次记载*/
@property(nonatomic,assign) BOOL isFirstLoad;

@end

@implementation XMMenuController


#pragma mark - 懒加载
- (NSMutableArray *)subControllers
{
    if (_subControllers == nil) {
        _subControllers = [NSMutableArray array];
    }
    return _subControllers;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置UI
    [self setupUI];
    
    //添加子控制器
    [self setupChildVc];
    
    //设置默认索引
    self.selectedIndex = 0;
    self.menuView.selectedIndex = 0;
    
    
    self.isFirstLoad = YES;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.isFirstLoad = NO;
}

- (void)setIsSideChange:(BOOL)isSideChange
{
    _isSideChange = isSideChange;
    
    self.scrollView.scrollEnabled = isSideChange;
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    self.menuView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 48);
    self.scrollView.frame = CGRectMake(0, 48, self.view.bounds.size.width, self.view.bounds.size.height - 48);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * self.subControllers.count, self.scrollView.bounds.size.height);
    
    CGFloat width = self.view.app_width / self.subControllers.count;
    if (self.isFirstLoad) {
//        self.indicatorView.frame = CGRectMake(10 + 0.5 * (self.menuView.app_width / self.subControllers.count - 44), self.menuView.bounds.size.height - 5, 44, 2);
        self.indicatorView.frame = CGRectMake(10 + 0.5 * (self.menuView.app_width / self.subControllers.count - width), self.menuView.bounds.size.height - 5, width, 2);
    }
    
    self.separtorView.frame = CGRectMake(0, self.menuView.bounds.size.height - 5, self.menuView.app_width, 2);

}

#pragma mark - 设置UI
/**设置UI*/
- (void)setupUI
{
    //背景色
    self.view.backgroundColor = kRGBColor(246, 246, 247);
    
    
    //添加子控件
    XMMenuView *menuView = [[XMMenuView alloc]init];
    menuView.delegate = self;
    [self.view addSubview:menuView];
    self.menuView = menuView;
    
    UIView *separtorView = [[UIView alloc] init];
    separtorView.backgroundColor = [UIColor clearColor];
    self.separtorView = separtorView;
    [menuView addSubview:separtorView];
    
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = kRGBColor(37, 179, 252);
    self.indicatorView = indicatorView;
    [menuView addSubview:indicatorView];
    
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
}

/**设置子控制器*/
- (void)setupChildVc
{
    UIViewController *vc1 = [[UIViewController alloc] init];
    vc1.view.backgroundColor = [UIColor redColor];
    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.view.backgroundColor = [UIColor greenColor];
    self.subControllers = [NSMutableArray arrayWithArray:@[vc1, vc2]];
}

#pragma mark - 事件监听
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    
    //加载指定的子控制器view
    [self kl_loadControllerViewAtIndex:selectedIndex];
}

/**加载指定索引的控制器视图*/
- (void)kl_loadControllerViewAtIndex:(NSUInteger)index
{
    
    if (self.subControllers.count == 0) {
        return;
    }
    
    if (index > self.subControllers.count - 1) {
        return;
    }
    
    //加载子视图
    UIViewController *vc = self.subControllers[index];
    vc.view.frame = CGRectMake(index * self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:vc.view];
    
    
    //滚动视图
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.app_width * index, 0) animated:YES];
}


#pragma mark - XMMenuViewDelegate
/**菜单按钮被点击*/
- (void)menuView:(XMMenuView *)menuView didMenuBtnClicked:(UIButton *)button
{
    self.selectedIndex = menuView.selectedIndex;
}

#pragma mark - UIScrollViewDelegate
/**停止滚动后*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //滚动偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    //当前的索引
    self.selectedIndex = offsetX / scrollView.app_width;
    
    //切换菜单栏
    self.menuView.selectedIndex = self.selectedIndex;
}

/**滚动的事件*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //计算当前的偏移量
    CGFloat offsetX = scrollView.contentOffset.x * (self.menuView.app_width - 20 - 20) / (scrollView.contentSize.width);
    
    //移动指示器
    self.indicatorView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
    
}

#pragma mark - XMHealthManageProtocol
- (void)viewController:(UIViewController *)sourceVc pushViewController:(UIViewController *)destVc {
    [self.navigationController pushViewController:destVc animated:YES];
}



@end
